package com.hndzyu.yd.vip;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.PDD;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.Util;

import java.util.List;

/**
 * Created by zz9527 on 2018/12/17.
 */
public class PDDAdapter  extends BaseAdapter {
    private final LayoutInflater inflater;
    private Context mContext;
    private List<PDD.DataBeanX.DataBean> list;

    public PDDAdapter(List<PDD.DataBeanX.DataBean> list, Context context) {
        this.list = list;
        this.mContext = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_pdditem, null);
            holder = new ViewHolder();
            holder.img = convertView.findViewById(R.id.iv_logo);
            holder.title = convertView.findViewById(R.id.tv_title);
            holder.qhprice = convertView.findViewById(R.id.tv_quan_hou_price);
            holder.nowprice = convertView.findViewById(R.id.tv_price_on_line);
            holder.yj = convertView.findViewById(R.id.tv_yong_price);
            holder.sell = convertView.findViewById(R.id.tv_sell_count);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        PDD.DataBeanX.DataBean bean = list.get(i);
        if (bean.getGoods_thumbnail_url().contains("http")){
            Glide.with(mContext).load(bean.getGoods_thumbnail_url()).into(holder.img);
        }else {
            Glide.with(mContext).load(Path.img(bean.getGoods_thumbnail_url())).into(holder.img);
        }
        holder.title.setText(bean.getGoods_name());
        double price=bean.getMin_group_price()/100;
        double qhprice=((double) bean.getMin_group_price()-(double)bean.getCoupon_discount())/100;
        String yj= Util.Sub(qhprice*((double)bean.getPromotion_rate()/1000)* (Double.parseDouble(UserBean.divide)+Double.parseDouble(UserBean.vipdivide))+"");
        holder.qhprice.setText(" "+qhprice+"元");
        holder.nowprice.setText("原价:" + price + "元");
        holder.yj.setText(yj + "元");
        holder.sell.setText("月销量:"+bean.getSold_quantity());
        return convertView;
    }

    private static class ViewHolder {
        ImageView img;
        TextView title;
        TextView qhprice;
        TextView nowprice;
        TextView yj;
        TextView sell;
    }
}

